#include<reg52.h>  // 包含8051微控制器的头文件

// 延时函数
void delay(unsigned int i) {
    unsigned int j;
    for (; i > 0; i--) {
        for (j = 0; j < 120; j++);  // 延时循环
    }
}

sbit ud = P2^6;  // 定义P2.6引脚为ud
sbit we = P2^7;  // 定义P2.7引脚为we

void main() {
    unsigned int n;  // 循环变量
    unsigned int a[4] = {0xef, 0xdf, 0xbf, 0x7f};  // 按键矩阵行扫描码
    unsigned int temp;
    unsigned char key = 0;  // 存储检测到的按键

    while (1) {  // 无限循环
        for (n = 0; n < 4; n++) {
            P3 = a[n];  // 将行扫描码设置到P3
            temp = P3;  // 读取P3
            temp = temp & 0xf0;  // 屏蔽低4位

            if (temp != 0xf0) {  // 如果有按键被按下
                delay(10);  // 防抖延时
                temp = P3;  // 再次读取P3
                temp = temp & 0xf0;  // 屏蔽低4位

                if (temp != 0xf0) {  // 如果按键仍然被按下
                    switch (temp) {
                        case 0xee: key = 1; break;  // 按键1
                        case 0xde: key = 2; break;  // 按键2
                        case 0xbe: key = 3; break;  // 按键3
                        case 0x7e: key = 4; break;  // 按键4
                        case 0xed: key = 5; break;  // 按键5
                        case 0xdd: key = 6; break;  // 按键6
                        case 0xbd: key = 7; break;  // 按键7
                        case 0x7d: key = 8; break;  // 按键8
                        case 0xeb: key = 9; break;  // 按键9
                        case 0xdb: key = 10; break; // 按键10（*）
                        case 0xbb: key = 11; break; // 按键11（0）
                        case 0x7b: key = 12; break; // 按键12（#）
                        case 0xe7: key = 13; break; // 按键13（A）
                        case 0xd7: key = 14; break; // 按键14（B）
                        case 0xb7: key = 15; break; // 按键15（C）
                        case 0x77: key = 16; break; // 按键16（D）
                    }

                    // 在七段显示器上显示对应的值
                    switch (key) {
                        case 1:
                            ud = 1;
                            P0 = 0x06;  // 显示1
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 2:
                            ud = 1;
                            P0 = 0x5b;  // 显示2
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 3:
                            ud = 1;
                            P0 = 0x4f;  // 显示3
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 4:
                            ud = 1;
                            P0 = 0x66;  // 显示4
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 5:
                            ud = 1;
                            P0 = 0x6d;  // 显示5
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 6:
                            ud = 1;
                            P0 = 0x7d;  // 显示6
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 7:
                            ud = 1;
                            P0 = 0x07;  // 显示7
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 8:
                            ud = 1;
                            P0 = 0x7f;  // 显示8
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 9:
                            ud = 1;
                            P0 = 0x6f;  // 显示9
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 10:
                            ud = 1;
                            P0 = 0x77;  // 显示*
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 11:
                            ud = 1;
                            P0 = 0x7c;  // 显示0
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 12:
                            ud = 1;
                            P0 = 0x39;  // 显示#
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 13:
                            ud = 1;
                            P0 = 0x5e;  // 显示A
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 14:
                            ud = 1;
                            P0 = 0x79;  // 显示B
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 15:
                            ud = 1;
                            P0 = 0x71;  // 显示C
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                        case 16:
                            ud = 1;
                            P0 = 0x3f;  // 显示D
                            ud = 0;
                            we = 1;
                            P0 = 0xc0;
                            we = 0;
                            delay(1);
                            break;
                    }
                }
            }
        }
    }
}
